package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.mapper.RealAuthMapper;
import cn.wolfcode.p2p.base.query.QueryObject;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.util.AssertUtil;
import cn.wolfcode.p2p.util.BitStatesUtil;
import cn.wolfcode.p2p.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RealAuthServiceImpl implements IRealAuthService {
    @Autowired
    private RealAuthMapper realAuthMapper;
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public void apply(RealAuth realAuth) {
        //判断基本参数
        //已经通过,不能实名认证
        //申请人
        LoginInfo applier = UserContext.getCurrentUser();
        UserInfo userInfo = userInfoService.getById(applier.getId());
        AssertUtil.instance().isFalse(userInfo.hasRealAuth(), "老铁,您已经通过了实名认证");
        //如果有一个实名申请,不能做实名认证
        AssertUtil.instance().isNull(userInfo.getRealAuthId(), "老铁,您已经有实名申请");
        //保存一个申请记录
        RealAuth ra = new RealAuth();
        ra.setRealName(realAuth.getRealName());
        ra.setSex(realAuth.getSex());
        ra.setIdNumber(realAuth.getIdNumber());
        ra.setBornDate(realAuth.getBornDate());
        ra.setAddress(realAuth.getAddress());
        ra.setImage1(realAuth.getImage1());
        ra.setImage2(realAuth.getImage2());
        ra.setApplier(applier);
        ra.setApplyTime(new Date());
        realAuthMapper.insert(ra);

        //修改user_info表里的realAuthId
        userInfo.setRealAuthId(ra.getId());
        userInfoService.update(userInfo);
    }

    @Override
    public RealAuth getById(Long realAuthId) {
        return realAuthMapper.selectByPrimaryKey(realAuthId);
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<RealAuth> list = realAuthMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void audit(Long id, int state, String remark) {
        //判断基本参数
        //判断为待审核状态
        RealAuth realAuth = realAuthMapper.selectByPrimaryKey(id);
        AssertUtil.instance().isFalse(realAuth.getState() != RealAuth.STATE_NORMAL, "实名认证对象状态不处于待审核");
        realAuth.setState(state);
        realAuth.setAuditor(UserContext.getCurrentUser());
        realAuth.setAuditTime(new Date());
        realAuth.setRemark(remark);
        update(realAuth);
        UserInfo userInfo = userInfoService.getById(realAuth.getApplier().getId());
        //审核成功
        if (state == RealAuth.STATE_SUCCESS) {
            userInfo.addBitState(BitStatesUtil.OP_REAL_AUTH);
            userInfo.setRealName(realAuth.getRealName());
            userInfo.setIdNumber(realAuth.getIdNumber());
        }else{
            userInfo.setRealAuthId(null);
        }
        userInfoService.update(userInfo);
    }
    @Override
    public void update(RealAuth realAuth){
        realAuthMapper.updateByPrimaryKey(realAuth);
    }
}
